#include "welcomewidget.h"
#include "ui_welcomewidget.h"
#include "page1_widget/page1_widget.h"
#include "page2_widget/page2_widget.h"
#include "page3_widget/page3_widget.h"
#include "page4_widget/page4_widget.h"
#include "page5_widget/page5_widget.h"

WelcomeWidget::WelcomeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WelcomeWidget)
{
    ui->setupUi(this);

    m_tx_thread = nullptr;
    m_serial_tx_thread = nullptr;

    m_rx_thread = nullptr;
    m_serial_rx_thread = nullptr;

    start_serial_port();

    m_485_1tx_thread = nullptr;
    m_rs485_1_tx_thread = nullptr;

    m_485_1rx_thread = nullptr;
    m_rs485_1_rx_thread = nullptr;

    start_rs485_1_serial_port();

    if((wireless_serial != nullptr) && (rs485_1_serial != nullptr))
    {
        page1_widget = new Page1_Widget(this);
        page2_widget = new Page2_Widget(this);
        page3_widget = new Page3_Widget(this);
        page4_widget = new Page4_Widget(this);
        page5_widget = new Page5_Widget(this);

	    base_timer = new QTimer(this);
	    connect(base_timer,SIGNAL(timeout()),this,SLOT(basetimer_timeout_handler()));
	    base_timer->start(TIMEOUT_BASE_VALUE);

	    update_information();

	    m_wifi = new WiFi_Connect();

        connect(page3_widget,SIGNAL(signal_motor_ctrl(bool)),page1_widget,SLOT(slot_pb_relay_ctrl_clicked(bool)));
        connect(page3_widget,SIGNAL(signal_light_ctrl(bool)),page1_widget,SLOT(slot_pb_led_ctrl_all_clicked(bool)));
        ui->pb_page_3->click();
	}
}


WelcomeWidget::~WelcomeWidget()
{
    delete page1_widget;
    delete page2_widget;
    delete page3_widget;
    delete page4_widget;
    delete page5_widget;

    delete base_timer;

    if(m_tx_thread)m_tx_thread->wait();
    if(m_rx_thread)m_rx_thread->wait();

    if(m_485_1tx_thread)m_485_1tx_thread->wait();
    if(m_485_1rx_thread)m_485_1rx_thread->wait();
    delete ui;
}


//通过定时器不断刷新当前时间
void WelcomeWidget::update_current_time()
{
    QDateTime current_time = QDateTime::currentDateTime();
    QString str_time = current_time.toString("yyyy-MM-dd hh:mm:ss dddd");

    ui->label_time->setText(str_time);
}

//定时器超时处理
void WelcomeWidget::basetimer_timeout_handler()
{
    static unsigned int time_update_count = 0;

    ++time_update_count;

    if(time_update_count > (TIMEOUT_CURRTIME_UPDATE / TIMEOUT_BASE_VALUE-1))
    {
        time_update_count = 0;
        update_current_time();
    }
}


void WelcomeWidget::on_pb_page_1_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->label_title->setText(ui->pb_page_1->text());

    page1_widget->send_get_dev_info_cmd();
}

void WelcomeWidget::on_pb_page_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->label_title->setText(ui->pb_page_2->text());
}

void WelcomeWidget::on_pb_page_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    ui->label_title->setText(ui->pb_page_3->text());
}

void WelcomeWidget::on_pb_page_4_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
    ui->label_title->setText(ui->pb_page_4->text());
}

void WelcomeWidget::on_pb_page_5_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
    ui->label_title->setText(ui->pb_page_5->text());
}


void WelcomeWidget::update_information()
{
    QString info;

    info = COMPANY_INFO;

    ui->label_information->setText(info);
}


void WelcomeWidget::start_serial_port()
{
    wireless_serial = nullptr;

    if(QFile::exists(USB_TTL_SERIAL_PORT))
    {
        wireless_serial = new Serial_Port(USB_TTL_SERIAL_PORT,QSerialPort::Baud115200);
        if(wireless_serial != nullptr)
        {
            start_serial_port_thread();
        }
        else
        {

        }
    }
}

void WelcomeWidget::start_serial_port_thread()
{
    if(m_tx_thread == nullptr)
    {
        m_tx_thread = new QThread();
        m_serial_tx_thread = new Serial_Tx_Thread(wireless_serial);
        m_serial_tx_thread->moveToThread(m_tx_thread);

        connect(m_tx_thread,SIGNAL(finished()),m_tx_thread,SLOT(deleteLater()));
        connect(m_tx_thread,SIGNAL(finished()),m_serial_tx_thread,SLOT(deleteLater()));

        connect(this,SIGNAL(signal_start_sp_tx_thread()),m_serial_tx_thread,SLOT(slot_serial_port_tx_thread()));

        m_tx_thread->start();

        emit signal_start_sp_tx_thread();
    }

    if(m_rx_thread == nullptr)
    {
        m_rx_thread = new QThread();
        m_serial_rx_thread = new Serial_Rx_Thread(wireless_serial);
        m_serial_rx_thread->moveToThread(m_rx_thread);

        connect(m_rx_thread,SIGNAL(finished()),m_rx_thread,SLOT(deleteLater()));
        connect(m_rx_thread,SIGNAL(finished()),m_serial_rx_thread,SLOT(deleteLater()));

        connect(this,SIGNAL(signal_start_sp_rx_thread()),m_serial_rx_thread,SLOT(slot_serial_port_rx_thread()));

        m_rx_thread->start();

        emit signal_start_sp_rx_thread();
    }
}




void WelcomeWidget::start_rs485_1_serial_port()
{
    rs485_1_serial = nullptr;

    if(QFile::exists(RS485_SERIAL_PORT))
    {
        rs485_1_serial = new Serial_Port(RS485_SERIAL_PORT,QSerialPort::Baud115200);

        if(rs485_1_serial != nullptr)
        {
            start_rs485_1_serial_port_thread();
        }
        else
        {

        }
    }
}

void WelcomeWidget::start_rs485_1_serial_port_thread()
{
    if(m_485_1tx_thread == nullptr)
    {
        m_485_1tx_thread = new QThread();
        m_rs485_1_tx_thread = new RS485_1_Tx_Thread(rs485_1_serial);
        m_rs485_1_tx_thread->moveToThread(m_485_1tx_thread);

        connect(m_485_1tx_thread,SIGNAL(finished()),m_485_1tx_thread,SLOT(deleteLater()));
        connect(m_485_1tx_thread,SIGNAL(finished()),m_rs485_1_tx_thread,SLOT(deleteLater()));

        connect(this,SIGNAL(signal_start_rs485_1_tx_thread()),m_rs485_1_tx_thread,SLOT(slot_rs485_1_port_tx_thread()));

        m_485_1tx_thread->start();

        emit signal_start_rs485_1_tx_thread();
    }

    if(m_485_1rx_thread == nullptr)
    {
        m_485_1rx_thread = new QThread();
        m_rs485_1_rx_thread = new RS485_1_Rx_Thread(rs485_1_serial);
        m_rs485_1_rx_thread->moveToThread(m_485_1rx_thread);

        connect(m_485_1rx_thread,SIGNAL(finished()),m_485_1rx_thread,SLOT(deleteLater()));
        connect(m_485_1rx_thread,SIGNAL(finished()),m_rs485_1_rx_thread,SLOT(deleteLater()));

        connect(this,SIGNAL(signal_start_rs485_1_rx_thread()),m_rs485_1_rx_thread,SLOT(slot_rs485_1_port_rx_thread()));

        m_485_1rx_thread->start();

        emit signal_start_rs485_1_rx_thread();
    }
}

